include Makefile.jenkins

VERSION=$(shell cat VERSION)
BUILD_DIR=build
BUILD_FILES=${BUILD_DIR} ${BUILD_DIR}/forest-${VERSION}.tar.gz
SOURCE_PACKAGE=${BUILD_DIR}/forest-${VERSION}.tar.gz
SOURCE_PACKAGE=${BUILD_DIR}/forest-${VERSION}.tar.gz
PYTHONSOLVRPM=repo/python-solv*.rpm
RPMRELEASENUM=1

VERBOSE=n
ifeq ($(VERBOSE), y)
QUIET_PREFIX=@
else
QUIET_PREFIX=
endif

ARCH=x86_64
MOCK_CONFIG=fedora-17-${ARCH}
MOCK=mock -r ${MOCK_CONFIG}

###### Closure stylesheets {{{
CLOSURE_STYLESHEETS_PATH=~/applications/closure-stylesheets-20111230.jar
CLOSURE_STYLESHEETS_FLAGS= --allowed-non-standard-function color-stop --pretty-print
CLOSURE_STYLESHEETS=java -jar ${CLOSURE_STYLESHEETS_PATH} ${CLOSURE_STYLESHEETS_FLAGS}
###### }}}

###### Echo debugging {{{
# Escape code
esc=$(shell echo -en "\033")

# Set colors
cc_red="${esc}[0;31m"
cc_green="${esc}[0;32m"
cc_yellow="${esc}[0;33m"
cc_normal=$(shell echo -en "${esc}[m\017")

define error_print
	echo -en "$(cc_red)${1}$(cc_normal)"
endef
define info_print
	@echo ${cc_green}${1}${cc_normal}
endef

define warning_print
	echo -en "$(cc_yellow)${1}$(cc_normal)"
endef
### }}}

# tests/test-run forest/bin/tree-build
all:	${BUILD_FILES} forest/server/static/main.css

${BUILD_DIR}:
	mkdir -p $@

# Source distribution. Mostly using setuptools
${SOURCE_PACKAGE}: ${BUILD_DIR}
	$(call info_print,Building source distribution...)
	@python setup.py --quiet sdist
	@mv dist/forest-${VERSION}.tar.gz build/
	@rm dist -rf

forest.iso:
		${MOCK} --shell "rm -rf /forest"
		#${MOCK} --shell "mkdir -p /forest"
#				 # copy source in
		${MOCK} --copyin ./ /forest/
		${MOCK} --install "PyYAML" --no-clean
		${MOCK} --install "python-setuptools" --no-clean
		${MOCK} --install "python-sqlalchemy" --no-clean
		${MOCK} --install "dbus-python" --no-clean
		${MOCK} --install "libuser-python" --no-clean
		${MOCK} --install "syslinux" --no-clean
		${MOCK} --install "genisoimage" --no-clean
		${MOCK} --install "parted" --no-clean
		${MOCK} --install "openssh" --no-clean
		${MOCK} --install "yum" --no-clean
		${MOCK} --install "python-solv" --no-clean
		${MOCK} --install "python-lxml" --no-clean
		${MOCK} --cwd="/forest" --shell "cd /forest && python -m forest.bin.build	 -d -v examples/installer/iso.tree -D yum.architecture=x86_64 -L DEBUG -l -"
		${MOCK} --copyout /forest/forest.iso forest.iso

forest.iso-embed:
	sudo python -m forest.bin.build examples/installer/iso.tree -D yum.architecture=x86_64 -l - -j forest.EmbedInstaller

STYLESHEETS=forest/server/static/main.gss

forest/server/static/main.css: ${STYLESHEETS}
	${CLOSURE_STYLESHEETS} --output-file $@		$^

develop:
	python setup.py develop

###### RPM RELATED

build/forest.spec: extras/forest.spec
	sed "s/Version:				 .*/Version: ${VERSION}/g" extras/forest.spec > $@
	sed -i "s/Release:				1%{?dist}/Release: ${RPMRELEASENUM}%{?dist}/g"	$@

# build locally
localbuild:
	python setup.py sdist
	cp extras/forest.spec ~/rpmbuild/SPECS
	cp dist/forest-0.4.tar.gz ~/rpmbuild/SOURCES
	cd ~/rpmbuild/SPECS && rpmbuild -bb forest.spec

# using mock
# 1. add user name to mock group:
# usermod -a -G mock myusername
#Create a config file that points to the repo(s) of your choice, where your test packages are
#mock -r <config-name> --init
#mock -r <config-name> --install <your packages>
#mock -r <config-name> --shell
mockbuild: ${SOURCE_PACKAGE} build/forest.spec
	$(call info_print,Building srpm...)
	${MOCK} --buildsrpm --spec build/forest.spec --sources build
	cp /var/lib/mock/${MOCK_CONFIG}/result/*src.rpm build/
	$(call info_print,Installing build deps...)
	${MOCK} --installdeps build/*src.rpm --no-cleanup-after
	$(call info_print,Building rpm...)
	${MOCK}	 --no-clean --rebuild build/*src.rpm
	cp /var/lib/mock/${MOCK_CONFIG}/result/*noarch.rpm build/
	# result is in /var/lib/mock/${MOCK_CONFIG}/result/

mockinstall: mockbuild
	${MOCK} --install build/forest-*noarch.rpm

mockshell:
	${MOCK} --shell

mockclean:
	${MOCK} --clean
	${MOCK} --init

mockiso:
	${MOCK} --shell "sed 's/treebuilder\.googlecode\.com\/hg\/examples/treebuilder.googlecode.com\/hg-history\/${VERSION}\/examples/g' -i /usr/share/forest/examples/installer/isolinux.cfg"
	${MOCK} --shell "sed 's/tree\.define=yum\.architecture=\$architecture/tree.define=yum.architecture=${ARCH}/g' -i /usr/share/forest/examples/installer/isolinux.cfg"
	${MOCK} --shell "python -m forest.bin.build	 -d -v /usr/share/forest/examples/installer/iso.tree -D yum.architecture=${ARCH} -L DEBUG -l -"

mocktestclient:
	${MOCK} --shell "yum install @core"				
	${MOCK} --copyin tests/client.yaml /etc/forest/client.yaml
	${MOCK} --shell "/usr/bin/python -m forest.client.main -c /etc/forest/client.yaml"
	
clean:
	sudo rm build* -rf
	sudo rm cache* -rf
	sudo rm installroot* -rf
	sudo rm isoroot* -rf
	rm dist -rf
	rm *egg-info -rf
	find ./ -name "*.pyc" | xargs rm

test:
	PYTHONPATH=$(shell pwd) /usr/bin/env nosetests -v -P forest/commands/disk

fulltest:
	# todo: figure out how to mock this
	/usr/bin/env nosetests tests

coverage:
	python `which nosetests1.1`		-w tests -w commands/disk -w commands/files -w commands/install -w commands/setup -w config -w config/resources	 -v -P	--with-coverage --cover-package forest
	
	
MOCK_ENV_PACKAGES=python-nose python-nose-cover3 python-coverage PyYAML python-setuptools python-sqlalchemy dbus-python libuser-python yum syslinux genisoimage parted openssh python-solv python-lxml
# creates a mock env
mockenvironment: ${PYTHONSOLVRPM} build/forest.spec
	$(call info_print,Preparing a mock environment...)
	${MOCK} -q --clean
	${MOCK} -q --init
	# install system deps
	${MOCK} -q --install ${MOCK_ENV_PACKAGES} --no-cleanup-after
	${MOCK} -q --install ${PYTHONSOLVRPM} --no-cleanup-after
	
mocktest: ${SOURCE_PACKAGE}
	$(call info_print,Running tests in a mock environment...)
	${MOCK} -q --shell "rm -rf /test/*"
	${MOCK} -q --shell "mkdir -p /test"
	# copy source in
	${MOCK} -q --copyin ${SOURCE_PACKAGE} /test/
	${MOCK} -q --cwd=/test --shell "cd /test && tar xvf *.tar.gz"
	# run tests. override pip to use the distros (fedora)
	${MOCK} -q --cwd=/test --shell "PIP=pip-python make -e -C /test/forest-${VERSION} test"
